package com.opencfg.lang;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.PumpStreamHandler;

/**
 * <p>
 * ProcessUtils
 * </p>
 * 
 * @author reymondtu@gmail.com
 * @version $Id: ProcessUtils.java 2011-10-20 $
 */
public final class ProcessUtils {
	
	/**
	 * Simple Execute Command Line
	 * 
	 * @param commandLine
	 * @return
	 * @throws IOException
	 */
	public final static boolean exec(final String commandLine) throws IOException {
		CommandLine cmdLine = CommandLine.parse(commandLine);
		DefaultExecutor executor = new DefaultExecutor();
		int exitValue = executor.execute(cmdLine);
		return executor.isFailure(exitValue);
	}
	
	/**
	 *  Execute Command Line With Define InputStream For Stand Input
	 *  
	 * @param commandLine
	 * @param in
	 * @return
	 * @throws IOException
	 */
	public final static boolean exec(final String commandLine, InputStream in) throws IOException {
		CommandLine cmdLine = CommandLine.parse(commandLine);
		DefaultExecutor executor = new DefaultExecutor();
		PumpStreamHandler pumpStreamHandler = new PumpStreamHandler();
		pumpStreamHandler.setProcessOutputStream(in);
		executor.setStreamHandler(pumpStreamHandler);
		int exitValue = executor.execute(cmdLine);
		return executor.isFailure(exitValue);
	}
	
	/**
	 *  Execute Command Line With Define Same OutputStream For Stand Out Put and Stand Error Put
	 *  
	 * @param commandLine
	 * @param in
	 * @return
	 * @throws IOException
	 */
	public final static boolean exec(final String commandLine, OutputStream outAndErr) throws IOException {
		CommandLine cmdLine = CommandLine.parse(commandLine);
		DefaultExecutor executor = new DefaultExecutor();
		executor.setStreamHandler(new PumpStreamHandler(outAndErr));
		int exitValue = executor.execute(cmdLine);
		return executor.isFailure(exitValue);
	}
	
	/**
	 * Execute Command Line With Define Different Two OutputStream For Stand Out Put And Stand Error Put
	 * 
	 * @param commandLine
	 * @param out
	 * @param err
	 * @return
	 * @throws IOException
	 */
	public final static boolean exec(final String commandLine, OutputStream out, OutputStream err) throws IOException {
		CommandLine cmdLine = CommandLine.parse(commandLine);
		DefaultExecutor executor = new DefaultExecutor();
		executor.setStreamHandler(new PumpStreamHandler(out, err));
		int exitValue = executor.execute(cmdLine);
		return executor.isFailure(exitValue);
	}
	
	/**
	 * Execute Command Line With Define Different Two OutputStream For Stand Out Put And Stand Error Put
	 * And Define InputStream For Input
	 * @param commandLine
	 * @param out
	 * @param err
	 * @param in
	 * @return
	 * @throws IOException
	 */
	public final static boolean exec(final String commandLine, OutputStream out, OutputStream err, InputStream in) throws IOException {
		CommandLine cmdLine = CommandLine.parse(commandLine);
		DefaultExecutor executor = new DefaultExecutor();
		executor.setStreamHandler(new PumpStreamHandler(out, err, in));
		int exitValue = executor.execute(cmdLine);
		return executor.isFailure(exitValue);
	}

}
